package com.jinmdz.fmis.api.wrapper;

import com.jinmdz.fmis.api.api.model.area.AreaOptionItem;
import com.jinmdz.fmis.api.constant.CacheKey;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.core.constant.Constants;
import com.jinmdz.fmis.core.model.OptionItem;
import com.jinmdz.fmis.core.util.CacheUtil;
import com.jinmdz.fmis.dao.dao.SystemAreaDao;
import com.jinmdz.fmis.dao.model.area.SystemAreaItem;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;

/**
 * AreaWrapper封装处理类
 *
 * @author LiCongLu
 * @date 2020-01-17 13:03
 */
@Component
public class AreaWrapper {

    @Resource
    private SystemAreaDao systemAreaDao;

    @Resource
    private Cache areaCache;

    /**
     * Bean初始化函数
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-17 13:05
     */
    @PostConstruct
    public void initMethod() {
        refreshArea();
    }

    /**
     * Bean销毁函数
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-19 11:16
     */
    @PreDestroy
    public void destroyMethod() {
        synchronized (areaCache) {
            // 清除原有缓存
            CacheUtil.clearCacheValue(areaCache, CacheKey.SYSTEM_AREA);
            CacheUtil.clearCacheValue(areaCache, CacheKey.SYSTEM_AREA_PROVINCE);
        }
    }

    /**
     * 刷新行政区划缓存
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-17 13:06
     */
    public void refreshArea() {
        // 获取所有行政区划
        ArrayList<SystemAreaItem> areaItems = systemAreaDao.listSystemAreaItem();
        Collections.sort(areaItems);

        // 省市县数据
        ArrayList<AreaOptionItem> provinceItems = new ArrayList<>();

        // 省级
        for (SystemAreaItem provinceArea : areaItems) {
            if (!Constants.AREA_CODE.equals(provinceArea.getParentCode())) {
                continue;
            }

            // 是否省市区县
            if (DataUtil.invalid(provinceArea.getAsProvinceCityCounty())) {
                continue;
            }

            AreaOptionItem provinceItem = BeanUtil.copy2Bean(getOptionItem(provinceArea), new AreaOptionItem());
            ArrayList<AreaOptionItem> cityItems = new ArrayList<>();

            //市级
            for (SystemAreaItem cityArea : areaItems) {
                if (!provinceArea.getAreaCode().equals(cityArea.getParentCode())) {
                    continue;
                }

                // 是否省市区县
                if (DataUtil.invalid(cityArea.getAsProvinceCityCounty())) {
                    continue;
                }

                AreaOptionItem cityItem = BeanUtil.copy2Bean(getOptionItem(cityArea), new AreaOptionItem());
                ArrayList<AreaOptionItem> countyItems = new ArrayList<>();

                //县级
                for (SystemAreaItem countyArea : areaItems) {
                    if (!cityArea.getAreaCode().equals(countyArea.getParentCode())) {
                        continue;
                    }

                    // 是否省市区县
                    if (DataUtil.invalid(countyArea.getAsProvinceCityCounty())) {
                        continue;
                    }

                    countyItems.add(BeanUtil.copy2Bean(getOptionItem(countyArea), new AreaOptionItem()));
                }
                Collections.sort(countyItems);
                cityItems.add(cityItem.setChildren(countyItems));
            }
            Collections.sort(cityItems);
            provinceItems.add(provinceItem.setChildren(cityItems));
        }
        Collections.sort(provinceItems);

        synchronized (areaCache) {
            // 清除原有缓存
            CacheUtil.clearCacheValue(areaCache, CacheKey.SYSTEM_AREA);
            CacheUtil.clearCacheValue(areaCache, CacheKey.SYSTEM_AREA_PROVINCE);
            areaCache.put(CacheKey.SYSTEM_AREA, areaItems);
            areaCache.put(CacheKey.SYSTEM_AREA_PROVINCE, provinceItems);
        }
    }

    /**
     * 转OptionItem
     *
     * @param areaItem 行政区划Item
     * @return
     * @author LiCongLu
     * @date 2020-01-19 10:29
     */
    private OptionItem getOptionItem(SystemAreaItem areaItem) {
        return new OptionItem().setId(areaItem.getId())
                .setValue(areaItem.getId())
                .setLabel(areaItem.getAreaName())
                .setAbbr(areaItem.getAreaNameAbbr())
                .setDisabled(areaItem.getDisableState())
                .setSort(areaItem.getSort());
    }

    /**
     * 获取所有行政区划
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-19 10:55
     */
    public ArrayList<SystemAreaItem> listSystemAreaItem() {
        ArrayList<SystemAreaItem> areaItems = new ArrayList<>();
        synchronized (areaCache) {
            ArrayList<SystemAreaItem> cacheItems = CacheUtil.getCacheValue(areaCache, CacheKey.SYSTEM_AREA);
            if (cacheItems != null && cacheItems.size() > 0) {
                areaItems = cacheItems;
            }
        }
        return areaItems;
    }

    /**
     * 获取省市县数据
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-01-19 10:41
     */
    public ArrayList<AreaOptionItem> listProvinceCityCounty() {
        ArrayList<AreaOptionItem> provinceItems = new ArrayList<>();
        synchronized (areaCache) {
            ArrayList<AreaOptionItem> cacheItems = CacheUtil.getCacheValue(areaCache, CacheKey.SYSTEM_AREA_PROVINCE);
            if (cacheItems != null && cacheItems.size() > 0) {
                provinceItems = cacheItems;
            }
        }
        return provinceItems;
    }

    /**
     * 获取子行政区划
     *
     * @param parentId             主键
     * @param asProvinceCityCounty 是否包含省市区县，-1全部;0,不包含;1只包含
     * @return
     * @author LiCongLu
     * @date 2020-01-19 10:52
     */
    public ArrayList<OptionItem> listAreaByParentId(Integer parentId, Integer asProvinceCityCounty) {
        ArrayList<OptionItem> optionItems = new ArrayList<>();
        ArrayList<SystemAreaItem> areaItems = listSystemAreaItem();
        for (SystemAreaItem areaItem : areaItems) {
            if (!areaItem.getParentId().equals(parentId)) {
                continue;
            }

            // 包含所有子行政区划
            if (asProvinceCityCounty == null || asProvinceCityCounty.intValue() < 0) {
                optionItems.add(getOptionItem(areaItem));
                continue;
            } else if (asProvinceCityCounty.intValue() == 0) {
                // 只包含非省市区县
                if (DataUtil.invalid(areaItem.getAsProvinceCityCounty())) {
                    optionItems.add(getOptionItem(areaItem));
                    continue;
                }
            } else {
                // 只包含省市区县
                if (DataUtil.valid(areaItem.getAsProvinceCityCounty())) {
                    optionItems.add(getOptionItem(areaItem));
                    continue;
                }
            }
        }
        Collections.sort(optionItems);
        return optionItems;
    }
}
